#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

class Request
{
public:
    Request()
    {
    }

    Request(int x, int y, int oper)
        : _x(x), _y(y), _oper(oper)
    {
    }

    ~Request()
    {
    }

    bool Serialize(std::string *out)
    {
        // 使用现成的库 jsoncpp
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;

        // Json::Value sub;
        // sub["age"] = 10;
        // sub["name"] = "张三";

        // root["sub"]  = sub;

        // Json::Value arr(Json::arrayValue);
        // arr.append(1);
        // arr.append(3);
        // arr.append(2);

        // root["arr"] = arr;

        Json::FastWriter w;
        // Json::StyledWriter w;
        std::string s = w.write(root);
        *out = s;
        return true;
    }

    void Deserialize(const std::string &in)
    {
        Json::Reader read;
        Json::Value root;
        read.parse(in, root);
        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();
    }

    void Print()
    {
        std::cout << _x << std::endl;
        std::cout << _y << std::endl;
        std::cout << _oper << std::endl;
    }

    int _x;
    int _y;
    char _oper;
};

int main()
{
    Request req;
    std::string json_string = "{\"oper\":43,\"x\":111,\"y\":222}";
    req.Deserialize(json_string);
    req.Print();

    return 0;
}